import {request} from "../../utils/request"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList:[],
    option1:[
      {text:"全部",value:2},
      { text: '喵星人', value: 0 },
      { text: '汪星人', value: 1 },
    ],
    option2:[
      {text:"全部",value:2},
      { text: '男生', value: 1 },
      { text: '女生', value: 0 },
    ],
    condition:{
      type:0,
      gender:0
    },
    keyword:""
  },
  toContext(e:any){
    let index = e.currentTarget.dataset.index
    let currentData = this.data.dataList[index]
    wx.navigateTo({
      url:"/pages/lyContext/lyContext",
      success(res){
        res.eventChannel.emit('context',currentData)
      }
    })
  },
  async typeChange(e:any){
    
      let name = e.currentTarget.dataset.name
      if(name=="gender"){
        this.setData({
          "condition.gender":e.detail
        })
      }
      if(name=="type"){
        this.setData({
          "condition.type":e.detail
        })
      }
      let condition = this.data.condition
      let result = await request('/song/filter',"GET",{
        ...condition
      }) as any
      console.log(result);
      
      this.setData({
        dataList:result.data
      })
    
  },
  onChange(e:any){
    if(e.detail==""){
      request('/song')
      .then((res:any)=>{
        this.setData({
          dataList:res.data
        })
      })
    }
    this.setData({
      keyword:e.detail
    })
  },
  async onSearch(){
    let keyword = this.data.keyword
    let resultData = await request('/song/keyword',"GET",{
      keyword
    }) as any
    this.setData({
      dataList:resultData.data
    })
  },
  onShow(){
    request('/song')
    .then((res:any)=>{
      let dataList = res.data.map((item:any)=>{
        item.createTime = item.createTime.substring(0,10)
        return item
      })
      this.setData({
        dataList
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    request('/song')
    .then((res:any)=>{
      this.setData({
        dataList:res.data
      })
    })
  },
})